package com.oa.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.DictItemUtils;
import com.oa.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.modules.sys.entity.TSysDictitemEntity;
import com.oa.modules.sys.service.TSysDictitemService;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;


/**
 * 系统字典表
 *
 * @author sunhui
 * @email admin
 * @date 2019-09-01 18:25:42
 */
@RestController
@RequestMapping("generator/tsysdictitem")
@Api(tags = "字典相关接口")
public class TSysDictitemController extends AbstractController {

    @Autowired
    private TSysDictitemService tSysDictitemService;


    /**
     * 获取字典组
     *
     * @param tSysDictitem
     * @return
     */
    @PostMapping("/getDictItemList")
    @ApiOperation(value = "查询字典组信息", notes = "groupId是字典的组号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "groupId是字典的组号", required = true, paramType = "query")
    })
    public R getDictItemList(@RequestBody TSysDictitemEntity tSysDictitem) {
        //return R.ok().put("data", DictItemUtils.dictItemsList.get(tSysDictitem.getGroupId()));
        if (StringUtils.isNotBlank(tSysDictitem.getDictValue()) && StringUtils.isNotBlank(tSysDictitem.getGroupId())) {
            return R.ok().put("data", tSysDictitemService.getMap(new QueryWrapper<TSysDictitemEntity>().eq("group_id", tSysDictitem.getGroupId()).eq("DICT_VALUE", tSysDictitem.getDictValue()).orderByDesc("`ORDER`")));
        } else if (StringUtils.isNotBlank(tSysDictitem.getGroupId())) {
            return R.ok().put("data", tSysDictitemService.listMaps(new QueryWrapper<TSysDictitemEntity>().eq("group_id", tSysDictitem.getGroupId()).orderByDesc("`ORDER`")));
        } else {
            return R.ok().put("data", tSysDictitemService.list());
        }
    }


    /**
     * 获取字典组
     *
     * @param tSysDictitem
     * @return
     */
    @PostMapping("/getDictItemListForWx")
    @ApiOperation(value = "查询字典组信息", notes = "groupId是字典的组号")
    public R getDictItemListForWx(@RequestBody TSysDictitemEntity tSysDictitem) {
        if (StringUtils.isNotBlank(tSysDictitem.getDictValue()) && StringUtils.isNotBlank(tSysDictitem.getGroupId())) {
            return R.ok().put("data", tSysDictitemService.getMap(new QueryWrapper<TSysDictitemEntity>().eq("group_id", tSysDictitem.getGroupId()).eq("DICT_VALUE", tSysDictitem.getDictValue()).orderByDesc("`ORDER`")));
        } else if (StringUtils.isNotBlank(tSysDictitem.getGroupId())) {
            return R.ok().put("data", tSysDictitemService.listMaps(new QueryWrapper<TSysDictitemEntity>().eq("group_id", tSysDictitem.getGroupId()).orderByDesc("`ORDER`")));
        } else {
            return R.ok().put("data", tSysDictitemService.listMaps(new QueryWrapper<TSysDictitemEntity>().orderByDesc("`ORDER`")));
        }
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    @RequiresPermissions("generator:tsysdictitem:list")
    @ApiOperation(value = "查询字典组信息", notes = "groupId是字典的组号")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tSysDictitemService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @PostMapping("/info/{dictId}")
    @RequiresPermissions("generator:tsysdictitem:info")
    public R info(@PathVariable("dictId") String dictId) {
        TSysDictitemEntity tSysDictitem = tSysDictitemService.getById(dictId);

        return R.ok().put("tSysDictitem", tSysDictitem);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("generator:tsysdictitem:save")
    public R save(@RequestBody TSysDictitemEntity tSysDictitem) {

        if (tSysDictitem == null || StringUtils.isBlank(tSysDictitem.getGroupId()) || StringUtils.isBlank(tSysDictitem.getDictValue()) || StringUtils.isBlank(tSysDictitem.getDictName())) {
            return R.error("确实必传参数");
        }

        tSysDictitem.setDictId(AppUtils.getUUID());
        tSysDictitem.setCreateDate(new Date());
        tSysDictitem.setCreateOper(getUserId().toString());
        tSysDictitemService.save(tSysDictitem);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("generator:tsysdictitem:update")
    public R update(@RequestBody TSysDictitemEntity tSysDictitem) {
        tSysDictitemService.updateById(tSysDictitem);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("generator:tsysdictitem:delete")
    public R delete(@RequestBody String[] dictIds) {
        tSysDictitemService.removeByIds(Arrays.asList(dictIds));

        return R.ok();
    }

}
